package com.youkeyi.ddy.cloud.application.system.dropdown.controller;

import com.youkeyi.ddy.cloud.common.api.application.system.dropdown.SystemDropdownCloudService;
import com.youkeyi.ddy.cloud.common.api.application.system.dropdown.dto.SystemDropdownListCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.system.dropdown.dto.SystemDropdownListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.system.dropdown.dto.SystemDropdownListItemKeyValueCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.system.dropdown.param.SystemDropdownListCloudParam;
import com.youkeyi.ddy.cloud.common.constant.ConstantContainer;
import com.youkeyi.ddy.cloud.common.constant.YoukeyiDdyCloudBasicStorageConstant;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * SystemDropdownController
 *
 * @Author liuxiawang
 * @Date 3/23/22 11:31 PM
 * @Location shenzhen.china
 */
@Service
public class SystemDropdownController implements SystemDropdownCloudService {

    // 放入常量
    private static Map<String, ConstantContainer> dropdown_map = new HashMap<>();
    static {
        dropdown_map.put("STORAGE_VACCINATION_GOODS_CATEGORY", YoukeyiDdyCloudBasicStorageConstant.STORAGE_VACCINATION_GOODS_CATEGORY);
    }

    @Override
    public SystemDropdownListCloudDto getOne(String dropdownName) {
        SystemDropdownListCloudDto dto = new SystemDropdownListCloudDto();
        if (StringUtils.isNotBlank(dropdownName)) {
            if (dropdown_map.containsKey(dropdownName)) {
                List<SystemDropdownListItemKeyValueCloudDto> items = setDropdownData(dropdown_map.get(dropdownName));
                if (CollectionUtils.isNotEmpty(items)) {
                    SystemDropdownListItemCloudDto row = new SystemDropdownListItemCloudDto();
                    row.setDropdownName(dropdownName);
                    row.setItems(items);
                    dto.getRows().add(row);
                }
            }
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public SystemDropdownListCloudDto getMore(SystemDropdownListCloudParam param) {
        SystemDropdownListCloudDto dto = new SystemDropdownListCloudDto();
        if (null != param && CollectionUtils.isNotEmpty(param.getDropdownNames())) {
            param.getDropdownNames().forEach(dropdownName -> {
                if (dropdown_map.containsKey(dropdownName)) {
                    List<SystemDropdownListItemKeyValueCloudDto> items = setDropdownData(dropdown_map.get(dropdownName));
                    if (CollectionUtils.isNotEmpty(items)) {
                        SystemDropdownListItemCloudDto row = new SystemDropdownListItemCloudDto();
                        row.setDropdownName(dropdownName);
                        row.setItems(items);
                        dto.getRows().add(row);
                    }
                }
            });
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public SystemDropdownListCloudDto getAll() {
        SystemDropdownListCloudDto dto = new SystemDropdownListCloudDto();
        if (dropdown_map.size() > 0) {
            dropdown_map.forEach((name, obj) -> {
                List<SystemDropdownListItemKeyValueCloudDto> items = setDropdownData(obj);
                if (CollectionUtils.isNotEmpty(items)) {
                    SystemDropdownListItemCloudDto row = new SystemDropdownListItemCloudDto();
                    row.setDropdownName(name);
                    row.setItems(items);
                    dto.getRows().add(row);
                }
            });
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    private List<SystemDropdownListItemKeyValueCloudDto> setDropdownData(ConstantContainer constantContainer) {
        if (null != constantContainer) {
            Map nameValuePairs = constantContainer.getNameValuePairs();
            if (null != nameValuePairs && nameValuePairs.size() > 0) {
                List<SystemDropdownListItemKeyValueCloudDto> items = new ArrayList<>();
                nameValuePairs.forEach((value, name) -> {
                    SystemDropdownListItemKeyValueCloudDto item = new SystemDropdownListItemKeyValueCloudDto();
                    if (null != value && null != name) {
                        item.setDropdownValue(value.toString());
                        item.setDropdownName(name.toString());
                        items.add(item);
                    }
                });
                if (CollectionUtils.isNotEmpty(items)) {
                    return items;
                }
            }
        }
        return null;
    }
}
